var uploader;
$(function(){
	activeNav($("#info-floor"));
	var ue = UE.getEditor('content');
	
	uploader = WebUploader.create({
		auto : false,
		swf : ctx + 'static/plugins/webuploader/Uploader.swf',
		server : ctx + "/api/upload/file",
		pick: { id : '#upload-images-btn', multiple : false },
		fileVal : 'file',
		duplicate : true,
		method : 'POST',
		fileSingleSizeLimit : 2*1024*1000,
		accept : {
		    title : '选择图片(*.jpg,*.gif,*.png)',
		    extensions : 'gif,jpg,jpeg,png',
		    mimeTypes : 'image/*'
		},
		thumb : {width: 100,height: 100}
	});
	
	uploader.on( 'error', function( type ) {
		switch (type) {
			case 'Q_TYPE_DENIED':
				$.alert({message:'请选择正确的图片类型！'});
				break;
			case 'Q_EXCEED_SIZE_LIMIT':
				$.alert({message:'文件大小不能超过2MB！'});
				break;
			default:
				break;
		}
	});
	
	uploader.on( 'fileQueued', function( file  ) {
		uploaderFlag = true;
		var files = uploader.getFiles();
		if(files.length > 1){
			uploader.reset();
			uploader.addFiles( file );
		}
		uploader.makeThumb( file, function( error, src ) {
			generateImageHtml(src,false);
		});
	});
	
	uploader.on('uploadBeforeSend',function(object ,data , headers ){
		data.folder = "/InformationImage/";
	});
	
	uploader.on( 'uploadAccept', function( object,ret ) {
		generateImageHtml(ret.filePath,true);
	});
	
	uploader.on( 'uploadFinished', function() {
		$('#form').submit();
	});
	
	$('#save-btn').click(function(){
		uploader.upload();
	});
	
	//加载页面
	generateImageHtml($('#image').val(),true);
});

function generateImageHtml(src,server){
	if(src != ''){
		var imgSrc = src;
		if(server){
			imgSrc = ctx + src;
			$('#image').val(src);
		}
		var html = '<img src="'+imgSrc+'" height="100" width="180" />'
		$(".filelist").html(html);
	}
}